/**
 * Created by tanbingqin on 2021/1/7.
 */
import React, { Component, PropTypes } from "react";
import relationUtil from "components/template/relation/relation-util";
import Chooser from "components/chooser";
import relationService from "components/template/relation/relation-service";

class RelationObj extends Component {
  state = {
    value: undefined,
    chooserConfig: null,
  };

  handleChange = (field, value, allValue) => {
    const { onChange, fields, form, formKey } = this.props;

    //判断state.value 和 value来确定是否真的change，不是onlyNeed的初始化onChange，清空value不做级联清除
    if (value && value !== this.state.value && !Array.isArray(this.state.value)) {
      let {
        result: relationFieldList,
        linkageFieldCodeFixList,
        linkageFieldCodeCustomList,
      } = relationUtil.findFieldsByFieldOID(field.fieldOID, fields);
      let params = {
        objId: value,
        relationType: JSON.parse(field.dataSource).relationType,
        linkageFieldCodeFixList,
        linkageFieldCodeCustomList,
      };
      relationService.getFieldData(params).then((res) => {
        let setData = relationUtil.getSetData(relationFieldList, res.data, formKey);
        if (JSON.stringify(setData) !== "{}") {
          form.setFieldsValue(setData);
        }
      });
    }
    this.setState({ value });
    onChange(value);
  };

  componentDidMount() {
    this.initChooserType();
    this.setState({ value: this.props.value });
  }

  componentWillReceiveProps(nextProps) {
    this.setState({ value: nextProps.value });
  }

  initChooserType = () => {
    const { field } = this.props;
    if (field.dataSource) {
      this.setState({ chooserConfig: relationUtil.getRelationTypeConfig(JSON.parse(field.dataSource).relationType) });
    }
  };

  renderExtraParams = () => {
    const { listExtraParams } = this.props;
    const { chooserConfig } = this.state;
    let extraParams = {};
    if (chooserConfig.isNeedExtraProcess) {
      Object.keys(chooserConfig.listExtraParams).forEach((key) => {
        extraParams[key] = listExtraParams[key] || undefined;
      });
    } else {
      extraParams = chooserConfig.listExtraParams;
    }
    return extraParams;
  };

  render() {
    const { field } = this.props;
    const { chooserConfig, value } = this.state;
    return (
      <div>
        {chooserConfig && (
          <Chooser
            type={chooserConfig.type}
            valueKey={chooserConfig.valueKey}
            placeholder={field.promptInfo}
            labelKey={chooserConfig.labelKey}
            onlyNeed={chooserConfig.onlyNeed}
            listExtraParams={this.renderExtraParams()}
            single
            newline
            disabled={field.isReadOnly}
            onChange={(e, all) => this.handleChange(field, e, all)}
            value={value}
          />
        )}
      </div>
    );
  }

  static propTypes = {
    field: PropTypes.object,
    fields: PropTypes.array,
  };

  static defaultProps = {
    field: {},
    fields: [],
  };
}

export default RelationObj;
